package com.bijava.test.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class GetNote_curator {

    // 创建会话
    public static void main(String[] args) throws Exception {


        //不使用fluent编程风格

        RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);

        // 使用fluent编程风格
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(50000)
                .connectionTimeoutMs(30000)
                .retryPolicy(exponentialBackoffRetry)
                //.namespace("base")  // 独立的命名空间 /base
                .build();

        client.start();


        // 创建节点
        String path = "/swifty-job";
        /*String s = client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath(path, "init".getBytes());

        System.out.println("节点递归创建成功，该节点路径" + s);

        // 获取节点的数据内容及状态信息

        // 数据内容
        byte[] bytes = client.getData().forPath(path);
        System.out.println("获取到的节点数据内容：" + new String(bytes));

        // 状态信息
        Stat stat = new Stat();
        client.getData().storingStatIn(stat).forPath(path);

        System.out.println("获取到的节点状态信息：" + stat );*/

        List<String> content = new ArrayList<>();
        List<String> childs = new ArrayList<>();
        getChildNode(client,path,childs);
        for (String child : childs) {
            byte[] bytes = client.getData().forPath(child);
            String result = new String(bytes, "utf-8");
            if (!StringUtils.isEmpty(result)){
                content.add("节点："+child+" 的内容为："+result);
            }
        }

        System.out.println("\r\n");
        for (String child : childs) {
            System.out.println(child);
        }

        System.out.println("\r\n");
        for (String s : content) {
            System.out.println(s);
        }
    }

    public static void getChildNode(CuratorFramework client,String path,List<String> childs){
        try {
            List<String> strings = client.getChildren().forPath(path);
            for (String string : strings) {
                String fullpath = path +"/"+ string;
                childs.add(fullpath);
                getChildNode(client,fullpath,childs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}